import java.util.LinkedList;
import java.util.Queue;

/**
 * @program: 1.15 栈和队列
 * @description:
 * @author: 李鑫月
 * @create: 2022-01-17 16:44
 * 用两个队列实现栈
 **/
class MyStack1 {
    Queue<Integer> queue1;
    Queue<Integer> queue2;
    public MyStack1() {
        queue1=new LinkedList<>();
        queue2=new LinkedList<>();
    }

    public void push(int x) {
        if(empty()){
            queue1.offer(x);
        }else {
            if(queue1.isEmpty()){
                queue2.offer(x);
            }else {
                queue1.offer(x);
            }
        }
    }

    public int pop() {
        if(empty()){
            return -1;
        }

        if(queue1.isEmpty()){
           int size= queue2.size()-1;
            for (int i = 0; i <size; i++) {
                    queue1.offer(queue2.peek());
                    queue2.poll();
            }

            return queue2.poll();

        }else{
            int size= queue1.size()-1;
            for (int i = 0; i <size; i++) {
                queue2.offer(queue1.peek());
                queue1.poll();
            }

            return queue1.poll();
        }

    }

    public int top() {
        if(empty()){
            return -1;
        }
        if(queue1.isEmpty()){
            int size= queue2.size()-1;

            for (int i = 0; i <size; i++) {
                queue1.offer(queue2.peek());
                queue2.poll();
            }
            int ret=queue2.peek();
            queue1.offer(queue2.poll());
            return ret;

        }else{
            int size= queue1.size()-1;

            for (int i = 0; i <size; i++) {
                queue2.offer(queue1.peek());
                queue1.poll();
            }
            int ret=queue1.peek();
            queue2.offer(queue1.poll());
            return ret;
        }


    }

    public boolean empty() {
        return queue1.isEmpty()&&queue2.isEmpty();
    }
}

